package com.web.controller;

import com.alibaba.fastjson2.JSON;
import com.web.entity.Course;
import com.web.entity.ResponseResult;
import com.web.entity.ResponseResult;
import com.web.service.CourseService;
import com.web.service.impl.CourseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Songzzz
 * @Description
 * @Date 2025/3/6 17:24
 */
@WebServlet("/addcourse")
public class AddCourseServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        //设置请求和响应编码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //设置响应类型
        resp.setContentType("application/json;charset=UTF-8");
        Integer id = Integer.parseInt(req.getParameter("id"));
        String name=req.getParameter("name");
        Integer time = Integer.parseInt(req.getParameter("time"));
        Integer point = Integer.parseInt(req.getParameter("point"));

        CourseService courseService=new CourseServiceImpl();
        Course course=courseService.addCourses(id,name,time,point);
        String str= JSON.toJSONString(new ResponseResult(200,"ok",course));
        resp.getWriter().write(str);
    }
}
